package br.com.controleVendas.utils;

import java.util.List;

import javax.faces.model.ListDataModel;

import org.primefaces.model.SelectableDataModel;

import br.com.controleVendas.entity.GenericDto;
import br.com.controleVendas.entity.GenericEntity;

public class DataModel<T extends GenericEntity<?>, D extends T> extends ListDataModel<D> implements SelectableDataModel<D>
{

    public DataModel()
    {
    }

    public DataModel(List<D> data)
    {
        super(data);
    }
    
    public void setWrappedData(Object list)
    {
        super.setWrappedData(list);
    }

    @SuppressWarnings("unchecked")
    @Override
    public D getRowData(String rowKey)
    {
        List<D> list = (List<D>) getWrappedData();

        for(D dto : list)
        {
            if(((GenericDto<T, D>) dto).getSelecionCdId().equals(rowKey))
                return dto;
        }

        return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Object getRowKey(D dto)
    {
        // TODO Auto-generated method stub
        return ((GenericDto<T, D>) dto).getSelecionCdId();
    }

}
